#include "TinyDBMysqlInterface.h"

CTinyDBMysqlInterface::CTinyDBMysqlInterface(void)
:m_conn(NULL),
m_driver(NULL)
{
	m_URL = "tcp://127.0.0.1:3306/tinycc";
	m_USER = "root";
	m_PASS = "tinycc";
}

CTinyDBMysqlInterface::~CTinyDBMysqlInterface(void)
{
}

bool CTinyDBMysqlInterface::openConnection(std::string connectionStr)
{
	if(connectionStr== "")
		return false;

	std::string otherpart;
	m_URL = connectionStr.substr(0,connectionStr.find(','));
	otherpart = connectionStr.substr(connectionStr.find(',')+1,std::string::npos);

	m_USER = otherpart.substr(0,otherpart.find(','));
	m_PASS = otherpart.substr(otherpart.find(',')+1,std::string::npos);

	try
	{
		m_driver = mysql::get_mysql_driver_instance();
		m_conn = m_driver->connect( m_URL,m_USER, m_PASS);
	}
	catch (sql::SQLException &e) {
		std::string error = e.what();
		return false;
	} 

	return true;
}

bool CTinyDBMysqlInterface::closeConnection()
{
	return false;
}

bool CTinyDBMysqlInterface::isConnectionOpened(void)
{
	if(m_conn != NULL)
		return true;
	return false;
}

sql::ResultSet* CTinyDBMysqlInterface::executeMysqlSQL(std::string &sql)
{
	if(m_conn == NULL)
		return NULL;	
	ResultSet * rset =NULL;
	try{
		Statement * stmt = m_conn->createStatement();
		rset = stmt->executeQuery(sql);
	}
	catch (sql::SQLException &e) {
		std::string error = e.what();
		return false;
	} 

	return rset;
}
void * CTinyDBMysqlInterface::executeSQL(std::string& sql)
{
	sql::ResultSet * rs = executeMysqlSQL(sql);
	return (void*)rs;
}